﻿using Magicodes.ExporterAndImporter.Excel.AspNetCore;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NP.BPMReportPlatform.AuthorizationUserManager.Dtos;
using NP.BPMReportPlatform.Entities.BPM_Process;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;

namespace NP.BPMReportPlatform.AuthorizationUserManager
{
    //[Authorize]
    [ApiController]
    [Route("api/AuthorizationRole")]
    public class AuthorizationRoleController : BPMReportPlatformController
    {
        private readonly IRoleAppService _roleAppService;
        public AuthorizationRoleController(IRoleAppService roleAppService) 
        {
            _roleAppService = roleAppService;
        }

        /// <summary>
        /// 获取角色信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetRoleList")]
        public async Task<PagedResultDto<Role>> GetRoleList([FromQuery] RoleInfoInput input)
        {
            return await _roleAppService.GetRoleList(input);
        }

        /// <summary>
        /// 导出角色信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("ExportRoleList")]
        public async Task<XlsxFileResult<Role>> ExportRoleList([FromBody] RoleInfoInput input)
        {
            var rst = await _roleAppService.ExportRoleList(input);
            return new XlsxFileResult<Role>(rst, "导出Excel");
        }

        /// <summary>
        /// 编辑角色
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("EditRole")]
        public async Task<string> EditRoleAsync([FromBody] Role input)
        {

            return await _roleAppService.EditRoleAsync(input);
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        [Route("DeleteRole")]
        public async Task<string> DeleteRoleAsync([FromQuery] Guid id)
        {
            return await _roleAppService.DeleteRoleAsync(id);
        }

        /// <summary>
        /// 角色授权流程
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        [HttpPost]
        [Route("AuthProcess")]
        public async Task<string> AuthProcess([FromBody] RoleBindProcessInput input)
        {
            return await _roleAppService.AuthProcess(input);
        }

        /// <summary>
        /// 获取流程树结构
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetAllProcesses")]
        public async Task<ProcessTreeData> GetAllProcesses(string roleId)
        {
            return await _roleAppService.GetAllProcesses(roleId);
        }

        /// <summary>
        /// 角色数据远程搜索
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetRoles")]
        public async Task<List<Role>> GetRoles(string name, int top = 50)
        {
            return await _roleAppService.GetRoles(name, top);
        }

        /// <summary>
        /// 获取用户绑定的角色
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetUserRoles")]
        public async Task<List<Role>> GetUserRoles(string workNumber)
        {
            return await _roleAppService.GetUserRoles(workNumber);
        }

        /// <summary>
        /// 用户授权角色
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("AuthUserRole")]
        public async Task<string> AuthUserRole([FromBody] BindUserRoleInput input)
        {
            return await _roleAppService.AuthUserRole(input);
        }
    }
}
